/****************************************************************************
 *
 *  ALi (Shanghai) Corporation, All Rights Reserved. 2008 Copyright (C)
 *
 *  File: win_imagepreview.c
 *
 *  Description: Mediaplayer image thumbnail preview UI.
 *
 *  History:
 *  Date            Author  		Version  Comment
 *  ====            ======  		=======  =======
 *  1.  2008.3.24   Sharon Zhang 	0.1.000  Initial
 *
 ****************************************************************************/
#include <sys_config.h>

#ifdef USB_MP_SUPPORT
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>

#include <api/libmp/media_player_api.h>
#include <api/libmp/pe.h>
#include <api/libimagedec/imagedec.h>
#include <api/libosd/osd_lib.h>
#include <hld/dis/vpo.h>

#include "control.h"
#include "osdobjs_def.h"
#include "images.id"
#include "string.id"
#include "osd_config.h"

#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_media.h"
/*******************************************************************************
*	Objects definition
*******************************************************************************/
#define MULTI_IMG_X_COUNT 3
#define MULTI_IMG_COUNT 9

#define IMAGEPREVIEW_HELPBAR_SUPPORT//ChengYu@20131101, image preview help bar

MATRIX_BOX_NODE mb_node_image[MULTI_IMG_COUNT];

UINT32 timer_preview = 0;
extern CONTAINER    g_win_imagepreview;

extern TEXT_FIELD	imgpreview_title;
extern MATRIX_BOX	imgpreview_mbx_img;
extern BITMAP		imgpreview_bmp_load;

#ifdef IMAGEPREVIEW_HELPBAR_SUPPORT
extern BITMAP		imagepreview_bmp_bak;
extern BITMAP		imagepreview_bmp_for;
extern TEXT_FIELD	imagepreview_txt_bak;
extern TEXT_FIELD	imagepreview_txt_for;
#endif

extern TEXT_FIELD 	imgpreview_txt_decode;
BOOL g_from_imagepreview = FALSE;

/*******************************************************************************
*	Function decalare
*******************************************************************************/
static VACTION preview_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT preview_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);
static VACTION preview_mbx_img_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT preview_mbx_img_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static void mbx_unkown_act_proc(VACTION act);
static void preview_scale_screen();
static void preview_init(void);
static void preview_image(UINT8 mbx_idx, UINT16 image_idx);
static void show_loadinglogo(UINT8 firstpos);
static void show_title(UINT16 idx);

#define PREVIEW_SH_IDX 	WSTL_NOSHOW_IDX
#define TITLE_SH_IDX	WSTL_MIXBACK_IDX_01_8BIT//sharon WSTL_TEXT_28_8BIT//WSTL_BUTTON_02_FG//WSTL_TEXT_25
#define MBX_IMG_SH_IDX	WSTL_MP_PREV_01
#define MBX_IMG_HL_IDX	WSTL_MP_PREV_02

#define BMP_LOAD_SH_IDX	WSTL_NOSHOW_IDX//WSTL_TRANS_IX
#define TXT_DECODE_SH_IDX	WSTL_TEXT_07

#define W_L  0
#define W_T  0
#define W_W  OSD_MAX_WIDTH
#define W_H  OSD_MAX_HEIGHT

#define TITLE_L	W_L
#define TITLE_T	W_T
#define TITLE_W	W_W
#ifdef IMAGEPREVIEW_HELPBAR_SUPPORT
#define TITLE_H	35
#else
#define TITLE_H	40
#endif

#ifdef IMAGEPREVIEW_HELPBAR_SUPPORT
#define MBX_IMG_GAP		4
#else
#define MBX_IMG_GAP		10
#endif
#if Maker_GUI_TEST
#define	MBX_IMG_L		TITLE_L //Martin@20150327
#else
#define	MBX_IMG_L		TITLE_L
#endif
#define	MBX_IMG_T		(TITLE_T+TITLE_H)

#ifdef IMAGEPREVIEW_HELPBAR_SUPPORT
#define	MBX_IMG_WIDTH	((W_W-MBX_IMG_L*2+MBX_IMG_GAP)/MULTI_IMG_X_COUNT-MBX_IMG_GAP)//169
#define	MBX_IMG_HEIGHT	((W_H-MBX_IMG_T+MBX_IMG_GAP-HELP_BMP_H)/(MULTI_IMG_COUNT/MULTI_IMG_X_COUNT)-MBX_IMG_GAP)//118
#else
#define	MBX_IMG_WIDTH	((W_W-MBX_IMG_L*2+MBX_IMG_GAP)/MULTI_IMG_X_COUNT-MBX_IMG_GAP)
#define	MBX_IMG_HEIGHT	((W_H-MBX_IMG_T+MBX_IMG_GAP)/(MULTI_IMG_COUNT/MULTI_IMG_X_COUNT)-MBX_IMG_GAP)
#endif

#define	MBX_IMG_W		MBX_IMG_WIDTH
#define	MBX_IMG_H		MBX_IMG_HEIGHT

#define	LOAD_W	60//42
#define	LOAD_H	60//42
#define LOAD_OFFSET_L	((MBX_IMG_WIDTH-LOAD_W)/2)
#define LOAD_OFFSET_T	((MBX_IMG_HEIGHT-LOAD_H)/2)
#define	LOAD_L	(MBX_IMG_L+LOAD_OFFSET_L)
#define	LOAD_T	(MBX_IMG_T+LOAD_OFFSET_T)

#define DEC_W	60
#define DEC_H	40
#define DEC_OFFSET_L	((MBX_IMG_WIDTH-DEC_W)/2)
#define DEC_OFFSET_T	((MBX_IMG_HEIGHT-DEC_H)/2)
#define DEC_L	(MBX_IMG_L+DEC_OFFSET_L)
#define DEC_T	(MBX_IMG_T+DEC_OFFSET_T)

#ifdef IMAGEPREVIEW_HELPBAR_SUPPORT
#define HELP_BMP_TXT_GAP	0
#define HELP_BAK_FOR_GAP	0

#define HELP_BAR_L	W_L+(W_W-(HELP_BMP_W+HELP_TXT_W+HELP_BMP_TXT_GAP)*2-HELP_BAK_FOR_GAP)/2
#define HELP_BAR_T	W_T+W_H-HELP_BMP_H

#define HELP_BMP_W	44
#define HELP_BMP_H	36

#define HELP_TXT_W	115
#define HELP_TXT_H	HELP_BMP_H
#endif

/*******************************************************************************
*	Objects Macro
*******************************************************************************/
#define LDEF_WIN(varCon,nxtObj,l,t,w,h,focusID)		\
	DEF_CONTAINER(varCon,NULL,NULL,C_ATTR_ACTIVE,0, \
	0,0,0,0,0, l,t,w,h, PREVIEW_SH_IDX,PREVIEW_SH_IDX,PREVIEW_SH_IDX,PREVIEW_SH_IDX,   \
	preview_keymap,preview_callback,  \
	nxtObj, focusID,0)

#define LDEF_TITLE(root,varTxt,nxtObj,l,t,w,h,str)		\
	DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,0, \
	0,0,0,0,0, l,t,w,h, TITLE_SH_IDX,TITLE_SH_IDX,TITLE_SH_IDX,TITLE_SH_IDX,   \
	NULL,NULL,  \
	C_ALIGN_CENTER| C_ALIGN_VCENTER, 0,0,0,str)

#define LDEF_MBX(root,varmb,nxtObj,l,t,w,h,cnt,row,col,pos)		\
    DEF_MATRIXBOX(varmb,&root,nxtObj,C_ATTR_ACTIVE,0, \
    1,1,1,1,1, l,t,w,h, MBX_IMG_SH_IDX,MBX_IMG_HL_IDX,MBX_IMG_SH_IDX,MBX_IMG_SH_IDX,   \
    preview_mbx_img_keymap,preview_mbx_img_callback ,  \
    MATRIX_ROW_WRAP, MATRIX_TYPE_BITMAP, cnt,mb_node_image, row, col,\
    C_ALIGN_CENTER | C_ALIGN_VCENTER, MBX_IMG_GAP,MBX_IMG_GAP,0,0,0,0,0)

#define LDEF_BMP_LOAD(root,varBmp,nxtObj,l,t,w,h,bmp_id)		\
	DEF_BITMAP(varBmp,&root,nxtObj, \
	C_ATTR_ACTIVE,C_FONT_DEFAULT, \
	0, 0, 0, 0, 0, \
	l, t, w, h, \
	BMP_LOAD_SH_IDX, BMP_LOAD_SH_IDX, BMP_LOAD_SH_IDX, BMP_LOAD_SH_IDX, \
	NULL,NULL,  \
	C_ALIGN_CENTER | C_ALIGN_VCENTER, 0, 0, bmp_id)

#ifdef IMAGEPREVIEW_HELPBAR_SUPPORT
#define LDEF_BMP(root,varbmp,nxtObj,l,t,w,h,bmpID)		\
    DEF_BITMAP(varbmp,&root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, WSTL_NOSHOW_IDX,WSTL_NOSHOW_IDX,WSTL_NOSHOW_IDX,WSTL_NOSHOW_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,bmpID)

#define LDEF_TXT(root,varTxt,nxtObj,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,C_FONT_1, \
    0,0,0,0,0, l,t,w,h, TITLE_SH_IDX,TITLE_SH_IDX,TITLE_SH_IDX,TITLE_SH_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)

#endif

/*******************************************************************************
*	Objects evaluate
*******************************************************************************/
LDEF_TITLE(g_win_imagepreview,imgpreview_title,&imgpreview_mbx_img, \
	TITLE_L,TITLE_T,TITLE_W,TITLE_H,display_strs[0])

#ifdef IMAGEPREVIEW_HELPBAR_SUPPORT
LDEF_MBX(g_win_imagepreview,imgpreview_mbx_img,&imagepreview_bmp_bak,\
	MBX_IMG_L,MBX_IMG_T,MBX_IMG_W,MBX_IMG_H,\
	MULTI_IMG_COUNT,MULTI_IMG_COUNT/MULTI_IMG_X_COUNT,MULTI_IMG_X_COUNT,0)
#else
LDEF_MBX(g_win_imagepreview,imgpreview_mbx_img,NULL,\
	MBX_IMG_L,MBX_IMG_T,MBX_IMG_W,MBX_IMG_H,\
	MULTI_IMG_COUNT,MULTI_IMG_COUNT/MULTI_IMG_X_COUNT,MULTI_IMG_X_COUNT,0)
#endif

LDEF_BMP_LOAD(g_win_imagepreview,imgpreview_bmp_load,NULL,	\
	LOAD_L,LOAD_T,LOAD_W,LOAD_H,IM_LOADING)

LDEF_WIN(g_win_imagepreview,&imgpreview_title,W_L,W_T,W_W, W_H, 1)

DEF_TEXTFIELD(imgpreview_txt_decode,NULL,NULL,C_ATTR_ACTIVE,C_FONT_1, \
	0,0,0,0,0, \
	DEC_L,DEC_T,DEC_W,DEC_H, \
	TXT_DECODE_SH_IDX,TXT_DECODE_SH_IDX,TXT_DECODE_SH_IDX,TXT_DECODE_SH_IDX,   \
	NULL,NULL,  \
	C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,0,display_strs[1])

#ifdef IMAGEPREVIEW_HELPBAR_SUPPORT
LDEF_BMP(g_win_imagepreview,imagepreview_bmp_bak,&imagepreview_txt_bak, \
	HELP_BAR_L, HELP_BAR_T, HELP_BMP_W, HELP_BMP_H, IM_MEDIAPLAY_CONTROL_BACKWARD_HI)
	
LDEF_TXT(g_win_imagepreview,imagepreview_txt_bak,&imagepreview_bmp_for, \
	HELP_BAR_L+HELP_BMP_W+HELP_BMP_TXT_GAP, HELP_BAR_T, HELP_TXT_W, HELP_TXT_H, RS_HELP_PAGE_UP, NULL)

LDEF_BMP(g_win_imagepreview,imagepreview_bmp_for,&imagepreview_txt_for, \
	HELP_BAR_L+(HELP_BMP_W*1+HELP_TXT_W)+HELP_BMP_TXT_GAP*1+HELP_BAK_FOR_GAP, HELP_BAR_T, HELP_BMP_W, HELP_BMP_H, IM_MEDIAPLAY_CONTROL_FORWARD_HI)

LDEF_TXT(g_win_imagepreview,imagepreview_txt_for,NULL, \
	HELP_BAR_L+(HELP_BMP_W*2+HELP_TXT_W)+HELP_BMP_TXT_GAP*2+HELP_BAK_FOR_GAP, HELP_BAR_T, HELP_TXT_W, HELP_TXT_H, RS_HELP_PAGE_DOWN, NULL)
#endif

/*******************************************************************************
*	local variable
*******************************************************************************/
UINT16 image_multiview_first;
UINT16 image_multiview_total;
UINT16 hl_index; //for title diaplay
static UINT8  image_idx_inpage,image_cnt_inpage;

extern BOOL from_imagepreview;
extern BOOL back_to_filelist;
extern PlayListHandle cur_playlist_image;
extern UINT8 image_folderloop_flag;

BOOL  preview_finished = FALSE;

/*******************************************************************************
*	Functions code
*******************************************************************************/

static VACTION preview_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act = VACT_PASS;

	switch(key)
	{
	case V_KEY_EXIT:
	case V_KEY_MENU:
		act = VACT_CLOSE;
		break;
	default:
		act = VACT_PASS;
	
	}

	return act;
}

static PRESULT preview_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	char progress_strs[20];
	static UINT16 callback_cnt = 0;
	UINT8 i;
	UINT16 curpos;
	FileInfo file_node;
	char image_name[FULL_PATH_SIZE] = {0};
	UINT32 file_idx;
	PRESULT ret = PROC_PASS;
    SYSTEM_DATA* sys_data=sys_data_get();

	switch(event)
	{
	case EVN_PRE_OPEN:
		if(from_imagepreview == TRUE && back_to_filelist == TRUE)
		{
			from_imagepreview = FALSE;
			back_to_filelist = FALSE;
			return PROC_LEAVE;
		}
    	if(TV_ASPECT_RATIO_169==sys_data_get_aspect_mode())
    	{
    		//MV_DEBUG("%s-->TvMode=%d\n",__FUNCTION__,SystemTvMode);
    		vpo_aspect_mode( (struct vpo_device *)dev_get_by_id(HLD_DEV_TYPE_DIS, 0), TV_4_3, NORMAL_SCALE);
		}
		api_set_preview_vpo_color(TRUE);
		preview_init();
		//imagedec_ioctl(1,IMAGEDEC_IO_CMD_FILL_LOGO,0x108080);
		//imagedec_ioctl(1,IMAGEDEC_IO_CMD_FILL_LOGO,0xB9748F);//yvu
        imagedec_ioctl(1, IMAGEDEC_IO_CMD_FILL_LOGO, 0x356B9D);//yvu
        image_abort();
		preview_image(image_idx_inpage, image_multiview_first + image_idx_inpage);
		show_title(hl_index);
		break;
	case EVN_POST_OPEN:
		show_loadinglogo(1);//not show first loading icon
		break;
	case EVN_PRE_DRAW:
		break;
	case EVN_UNKNOWN_ACTION:
		break;
	case EVN_PRE_CLOSE:
		g_from_imagepreview = TRUE;
		if(TV_ASPECT_RATIO_169==sys_data_get_aspect_mode())
		{
			win_av_set_ratio(sys_data,3);
			sys_data_set_display_mode(&(sys_data->avset));	
		}		
		if(image_folderloop_flag == 1)//filelist
		{
			curpos = OSD_GetMtrxboxCurPos(&imgpreview_mbx_img);
			get_file_from_play_list(cur_playlist_image, (image_multiview_first+curpos), &file_node);
			win_get_fullname(image_name, file_node.path, file_node.name);
			usblist_set_filelist_param(image_name);
		}
		break;
	case EVN_POST_CLOSE:
		image_abort();
		from_imagepreview = FALSE;
		vpo_win_onoff((struct vpo_device *)dev_get_by_id(HLD_DEV_TYPE_DIS, 0), FALSE);
#ifdef DUAL_VIDEO_OUTPUT        
    	vpo_win_onoff((struct vpo_device *)dev_get_by_id(HLD_DEV_TYPE_DIS, 1), FALSE);
#endif
		OSD_ClearObject((POBJECT_HEAD)&imgpreview_title,C_UPDATE_ALL);
		OSD_ClearObject((POBJECT_HEAD)&imgpreview_mbx_img,C_UPDATE_ALL);
		break;
	case EVN_MSG_GOT:
        if(param1 == CTRL_MSG_SUBTYPE_CMD_STO && param2 == USB_STATUS_OVER)
        {
			//storage_dev_mount_hint(1);
			file_list_check_storage_device(FALSE, FALSE);
        }
		
		//show dec percent
		else if ((param1 == CTRL_MSG_SUBTYPE_STATUS_JPEG_DEC_PROCESS) || (param1 == CTRL_MSG_SUBTYPE_STATUS_IMAGEOVER))
		{
		//	if(CTRL_MSG_SUBTYPE_STATUS_IMAGEOVER == param1)
		//		SDBBP();
			if ((param2 < 100) && (param1 == CTRL_MSG_SUBTYPE_STATUS_JPEG_DEC_PROCESS))
			{
				callback_cnt++;
				sprintf(progress_strs,"%d%%.",param2);
				for(i = 0;i < (callback_cnt%3);i++)
				{
					sprintf(progress_strs,"%s%s",progress_strs,".");
				}
				OSD_SetTextFieldContent(&imgpreview_txt_decode,STRING_ANSI,(UINT32)progress_strs);
				OSD_DrawObject((POBJECT_HEAD)&imgpreview_txt_decode,C_UPDATE_ALL);
			}
			else if((param2 >= 100) || (param1 == CTRL_MSG_SUBTYPE_STATUS_IMAGEOVER))
			{
				OSD_ClearObject((POBJECT_HEAD)&imgpreview_txt_decode,C_UPDATE_ALL);
			//	OSD_ObjClose((POBJECT_HEAD)&imgpreview_txt_decode,C_UPDATE_ALL);
			}
		}

		if(param1 == CTRL_MSG_SUBTYPE_STATUS_IMAGEOVER)
		{
            if(param2 > timer_preview)
            {
    			image_abort();
            }
            else
            {
                libc_printf("@@@@@@@@@@@@@@ es case\n");
                break;
            }
			image_idx_inpage++;
			if(image_idx_inpage < image_cnt_inpage)
			{
				preview_image(image_idx_inpage, image_multiview_first + image_idx_inpage);
			}
		}
		else if(param1 == CTRL_MSG_SUBTYPE_STATUS_MP3OVER)
		{
			win_play_next_music_ex();
		}
		else if(param1 == CTRL_MSG_SUBTYPE_CMD_UPDATE_VIEW)
		{

		}
		else {}

		break;
	default:
		break;
	}

	return ret;
}

static VACTION preview_mbx_img_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION	act = VACT_PASS;

	switch(key)
	{
	case V_KEY_P_UP:
	case V_KEY_PREV://ChengYu@20131101, image preview page up/down
		act = VACT_CURSOR_PGUP;
		break;
	case V_KEY_P_DOWN:
	case V_KEY_NEXT://ChengYu@20131101, image preview page up/down
		act = VACT_CURSOR_PGDN;
		break;
	case V_KEY_UP:
		act = VACT_CURSOR_UP;
		break;
	case V_KEY_DOWN:
		act = VACT_CURSOR_DOWN;
		break;
	case V_KEY_LEFT:
		act = VACT_CURSOR_LEFT;
		break;
	case V_KEY_RIGHT:
		act = VACT_CURSOR_RIGHT;
		break;
	case V_KEY_ENTER:
		act = VACT_ENTER;
		break;
	default:
		act = VACT_PASS;
		break;
	}
	return act;
}

static PRESULT preview_mbx_img_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	VACTION unact;
	PMATRIX_BOX pmbx = (PMATRIX_BOX)pObj;
	UINT16 curpos;

	switch(event)
	{
		case EVN_ITEM_PRE_CHANGE:
			break;
		case EVN_ITEM_POST_CHANGE:
			show_loadinglogo(image_idx_inpage+1);
			curpos = OSD_GetMtrxboxCurPos(pmbx);
			show_title(image_multiview_first + curpos);
			break;
		case EVN_UNKNOWN_ACTION:
			unact = (VACTION)(param1>>16);
			mbx_unkown_act_proc(unact);
			break;
		default:
			break;
	}
	
	return ret;
}

static void mbx_unkown_act_proc(VACTION act)
{
	POBJECT_HEAD win;
	PMATRIX_BOX pmbx = &imgpreview_mbx_img;
	UINT16 curpos;
	
	switch(act)
	{
		case VACT_CURSOR_PGUP:
		case VACT_CURSOR_PGDN:
			if(act == VACT_CURSOR_PGUP)
			{
				if(image_multiview_first <=1 )
				{
					break;
				}
				else if(image_multiview_first > MULTI_IMG_COUNT)
				{
					image_multiview_first -= MULTI_IMG_COUNT;
					image_cnt_inpage = MULTI_IMG_COUNT;
				}
				else
				{
					image_multiview_first = 1;
					image_cnt_inpage = (image_multiview_total<MULTI_IMG_COUNT)?image_multiview_total:(MULTI_IMG_COUNT);
				}
			}
			else //(act == VACT_CURSOR_PGDN)
			{
				if(image_multiview_first <= image_multiview_total - MULTI_IMG_COUNT)
				{
					image_multiview_first += MULTI_IMG_COUNT;
					image_cnt_inpage = ((image_multiview_total-image_multiview_first+1) < MULTI_IMG_COUNT)? \
									(image_multiview_total-image_multiview_first+1):(MULTI_IMG_COUNT);
				}
				else
					break;
			}
#if 1//ChengYu@20131101, fix change to the last page before this page not loading finished will cause redundant Im_Loading bitmap on screen
			OSD_ClearObject((POBJECT_HEAD)&g_win_imagepreview,0);
			OSD_DrawObject((POBJECT_HEAD) &g_win_imagepreview, C_UPDATE_ALL);
#endif
			OSD_SetMtrxboxCount(pmbx, image_cnt_inpage, MULTI_IMG_COUNT/MULTI_IMG_X_COUNT, MULTI_IMG_X_COUNT);
			curpos = OSD_GetMtrxboxCurPos(pmbx);
			if(curpos > image_cnt_inpage-1)
			{
				curpos = image_cnt_inpage-1;
				OSD_SetMtrxboxCurPos(pmbx, curpos);
			}
			OSD_TrackObject((POBJECT_HEAD)pmbx,C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL | C_DRAW_TYPE_HIGHLIGHT);
			hl_index = image_multiview_first + curpos;

			show_loadinglogo(1);
			//imagedec_ioctl(1,IMAGEDEC_IO_CMD_FILL_LOGO,0xB9748F);//yvu
      	  	imagedec_ioctl(1, IMAGEDEC_IO_CMD_FILL_LOGO, 0x356B9D);//yvu
			image_abort();
            timer_preview = osal_get_tick();
			image_idx_inpage = 0;
			preview_image(image_idx_inpage, image_multiview_first + image_idx_inpage);
			show_title(hl_index);
			break;
		case VACT_ENTER:
			win = (POBJECT_HEAD)(&g_win_imagepreview);
			OSD_ObjClose(win,C_CLOSE_CLRBACK_FLG);

			image_abort();
			from_imagepreview = TRUE;
			curpos = OSD_GetMtrxboxCurPos(pmbx);
			win_set_image_slide_idx(image_multiview_first+curpos);
			win = (POBJECT_HEAD)(&g_win_imageslide);
			if(OSD_ObjOpen(win,(UINT32)(~0)) != PROC_LEAVE)
				menu_stack_push(win);
			break;
		default:
			break;
	}
}

static void preview_init(void)
{
	UINT8 i;
	UINT16 curpos;
	UINT32 dir_num,file_num;

	//alloc bmp
	for(i = 0; i < MULTI_IMG_COUNT; i++)
	{
		mb_node_image[i].str = 0;
		mb_node_image[i].num = 0;//IM_LOADING;
		mb_node_image[i].bAttr = C_ATTR_ACTIVE;
	}

	//init parametre
	get_play_list_info(cur_playlist_image,&file_num, NULL);
	image_multiview_total = file_num;

	image_idx_inpage = 0;

	if((image_multiview_total - image_multiview_first + 1) >= MULTI_IMG_COUNT)
	{
		curpos = 0;
		image_cnt_inpage = MULTI_IMG_COUNT;
	}
	else if(image_multiview_total <= MULTI_IMG_COUNT)
	{
		curpos = image_multiview_first-1;
		image_multiview_first = 1;
		image_cnt_inpage = image_multiview_total;
	}
	else
	{
		curpos = MULTI_IMG_COUNT - (image_multiview_total-image_multiview_first)-1;
		image_multiview_first = image_multiview_total - MULTI_IMG_COUNT + 1;
		image_cnt_inpage = MULTI_IMG_COUNT;
	}

	hl_index = image_multiview_first + curpos;

	OSD_SetMtrxboxCount(&imgpreview_mbx_img, image_cnt_inpage, MULTI_IMG_COUNT/MULTI_IMG_X_COUNT, MULTI_IMG_X_COUNT);
	OSD_SetMtrxboxCurPos(&imgpreview_mbx_img, curpos);
}

static void preview_image(UINT8 mbx_idx, UINT16 image_idx)
{
	UINT16 Col, Row;
    UINT8 tv_mode;
	RECT rect_info;
	RET_CODE ret=RET_SUCCESS;
	UINT16 left, top;
	FileInfo file_node;
	char full_name[FULL_PATH_SIZE] = {0};
    char *ext;

    UINT16  img_top, img_height, img_gap_w, img_gap_h, img_left, img_width;

    struct OSDRect osd_rect;
    OSD_GetRectOnScreen(&osd_rect);

#ifndef SD_UI
    img_left = (MBX_IMG_L + osd_rect.uLeft) * 720/1280;
    img_width = (MBX_IMG_WIDTH)*720/1280;//+2;
    img_gap_w = MBX_IMG_GAP*720/1280;
    img_gap_h = MBX_IMG_GAP*576/720;

	OSD_GetMtrxBoxCellLocation(&imgpreview_mbx_img, mbx_idx, &Col, &Row);

	left =  MBX_IMG_L + (MBX_IMG_WIDTH+MBX_IMG_GAP)*Col;
    top = MBX_IMG_T + (MBX_IMG_HEIGHT+MBX_IMG_GAP)*Row;
	OSD_MoveObject((POBJECT_HEAD)&imgpreview_bmp_load,left+LOAD_OFFSET_L,top+LOAD_OFFSET_T,FALSE);
	OSD_ClearObject((POBJECT_HEAD)&imgpreview_bmp_load,C_UPDATE_ALL);

	OSD_MoveObject((POBJECT_HEAD)&imgpreview_txt_decode,left+DEC_OFFSET_L,top+DEC_OFFSET_T,FALSE);

    tv_mode = api_video_get_tvout(); 
    
    if(TV_MODE_PAL == tv_mode || TV_MODE_720P_50 == tv_mode || TV_MODE_1080I_25 == tv_mode)
    {
    //    rect_info.top = MBX_IMG_T + OSD_STARTROW_P + (MBX_IMG_HEIGHT+MBX_IMG_GAP)*Row +4;
    //    rect_info.bottom = rect_info.top + MBX_IMG_HEIGHT-8;
          
          img_top = (MBX_IMG_T + osd_rect.uTop)*576/720;
          img_height = MBX_IMG_H*576/720;
          rect_info.top = img_top + (img_height+img_gap_h)*Row + 4;
          rect_info.bottom = rect_info.top + img_height - 8;
    }
    else 
    {
    //    rect_info.top = (MBX_IMG_T + OSD_STARTROW_N + (MBX_IMG_HEIGHT+MBX_IMG_GAP)*Row)*576/480+4;
	//    rect_info.bottom = rect_info.top + MBX_IMG_HEIGHT*576/480-8;  

          img_top = (MBX_IMG_T + osd_rect.uTop)*480/720;
          img_height = MBX_IMG_H*480/720;
          rect_info.top = (img_top + (img_height + img_gap_h) * Row) * 576/480 + 4;
          rect_info.bottom = rect_info.top + img_height * 576/480 - 8;         
    }
//	rect_info.left =  OSD_STARTCOL + MBX_IMG_L + (MBX_IMG_WIDTH+MBX_IMG_GAP)*Col+4;
//	rect_info.right = rect_info.left + MBX_IMG_WIDTH-8;
    rect_info.left = img_left + (img_width + img_gap_w) * Col + 4;
    rect_info.right = rect_info.left + img_width - 8;
#ifdef BIDIRECTIONAL_OSD_STYLE
	if (OSD_GetMirrorFlag() == TRUE)
	{
		rect_info.left = 720 - rect_info.right -4;
//		rect_info.left = (OSD_HD_STARTCOL + (OSD_HD_MAX_WIDTH - (MBX_IMG_L + (MBX_IMG_WIDTH+MBX_IMG_GAP)*Col) - MBX_IMG_WIDTH))*720/1280 +2 ;
        rect_info.right = rect_info.left + img_width - 8;
	}
#endif

#else
    img_left = (MBX_IMG_L + osd_rect.uLeft) ;
    img_width = (MBX_IMG_WIDTH);//+2;
    img_gap_w = MBX_IMG_GAP;
    img_gap_h = MBX_IMG_GAP;

	OSD_GetMtrxBoxCellLocation(&imgpreview_mbx_img, mbx_idx, &Col, &Row);

	left =  MBX_IMG_L + (MBX_IMG_WIDTH+MBX_IMG_GAP)*Col;
    top = MBX_IMG_T + (MBX_IMG_HEIGHT+MBX_IMG_GAP)*Row;
	OSD_MoveObject((POBJECT_HEAD)&imgpreview_bmp_load,left+LOAD_OFFSET_L,top+LOAD_OFFSET_T,FALSE);
	OSD_ClearObject((POBJECT_HEAD)&imgpreview_bmp_load,C_UPDATE_ALL);

	OSD_MoveObject((POBJECT_HEAD)&imgpreview_txt_decode,left+DEC_OFFSET_L,top+DEC_OFFSET_T,FALSE);

    tv_mode = api_video_get_tvout(); 
    
    if(TV_MODE_PAL == tv_mode || TV_MODE_576P == tv_mode)
    {
    //    rect_info.top = MBX_IMG_T + OSD_STARTROW_P + (MBX_IMG_HEIGHT+MBX_IMG_GAP)*Row +4;
    //    rect_info.bottom = rect_info.top + MBX_IMG_HEIGHT-8;
          
          img_top = (MBX_IMG_T + osd_rect.uTop);
          img_height = MBX_IMG_H;
          rect_info.top = img_top + (img_height+img_gap_h)*Row + 4;
          rect_info.bottom = rect_info.top + img_height - 8;
    }
    else 
    {
    //    rect_info.top = (MBX_IMG_T + OSD_STARTROW_N + (MBX_IMG_HEIGHT+MBX_IMG_GAP)*Row)*576/480+4;
	//    rect_info.bottom = rect_info.top + MBX_IMG_HEIGHT*576/480-8;  

          img_top = (MBX_IMG_T + osd_rect.uTop)*480/576;
          img_height = MBX_IMG_H*480/576;
          rect_info.top = (img_top + (img_height + img_gap_h) * Row) * 576/480 + 4;
          rect_info.bottom = rect_info.top + img_height * 576/480 - 8;         
    }
//	rect_info.left =  OSD_STARTCOL + MBX_IMG_L + (MBX_IMG_WIDTH+MBX_IMG_GAP)*Col+4;
//	rect_info.right = rect_info.left + MBX_IMG_WIDTH-8;
    rect_info.left = img_left + (img_width + img_gap_w) * Col + 4;
    rect_info.right = rect_info.left + img_width - 8;
#endif
	get_file_from_play_list(cur_playlist_image,image_idx,&file_node);
	win_get_fullname(full_name, file_node.path, file_node.name);
    ext = strrchr(file_node.name, '.');
    if(ext && strncasecmp(ext, ".png", 4) && strncasecmp(ext, ".gif", 4))
	win_image_file_preview(full_name, rect_info, IMAGEDEC_MULTI_PIC);
//    api_set_vpo_dit(TRUE);
}

static void show_loadinglogo(UINT8 firstpos)
{
	UINT8 i;
	UINT16 Col,Row;
	UINT16 top, left;
	PBITMAP pbitmap;

	pbitmap = &imgpreview_bmp_load;
	for(i=firstpos; i<MULTI_IMG_COUNT; i++)
	{
		OSD_GetMtrxBoxCellLocation(&imgpreview_mbx_img, i, &Col, &Row);
		left =  MBX_IMG_L + (MBX_IMG_WIDTH+MBX_IMG_GAP)*Col;
        top = MBX_IMG_T + (MBX_IMG_HEIGHT+MBX_IMG_GAP)*Row;

		OSD_MoveObject((POBJECT_HEAD)pbitmap,left+LOAD_OFFSET_L,top+LOAD_OFFSET_T,FALSE);
        if (i < image_cnt_inpage)
    		OSD_DrawObject((POBJECT_HEAD)pbitmap,C_UPDATE_ALL);
        else
    		OSD_ClearObject((POBJECT_HEAD)pbitmap,C_UPDATE_ALL);
	}
}

static void show_title(UINT16 idx)
{
	char str_name[FULL_PATH_SIZE] = {0};
    char str_buff[FULL_PATH_SIZE] = {0};
	char full_name[FULL_PATH_SIZE] = {0};
	TEXT_FIELD *pTxt;
	FileInfo file_node;
	#ifdef PERSIAN_SUPPORT
	UINT16 str[64];		
	char str_uni[128];
	#endif
	if(image_folderloop_flag == 1)//filelist
	{
		STRCPY(str_buff, "Disk ");
		get_file_from_play_list(cur_playlist_image, idx, &file_node);
		win_get_fullname(full_name, file_node.path, file_node.name);
	#ifdef PERSIAN_SUPPORT
		filelist_utf8_to_unicode(full_name, str_uni);
    	ComUniStrCopyChar( (UINT8 *)str, str_uni);
	#else
		win_get_display_dirname(str_name, full_name);
		strcat(str_buff, str_name);
	#endif
	}
	else//playlist
	{
	#ifndef _USE_32M_MEM_
		STRCPY(str_buff, "Image Play List/");
	#else
		STRCPY(str_buff, " ");
	#endif
		get_file_from_play_list(cur_playlist_image,idx,&file_node);
	#ifdef PERSIAN_SUPPORT
		filelist_utf8_to_unicode(file_node.name, str_uni);
    	ComUniStrCopyChar( (UINT8 *)str, str_uni);
	#else
		//filter_unkownfont_name(file_node.name,str_name);
		win_shorten_filename(file_node.name,str_name);
		strcat(str_buff, str_name);
	#endif
	}
	
	pTxt = &imgpreview_title;
	OSD_ClearObject((POBJECT_HEAD)pTxt,C_UPDATE_ALL);
	#ifdef PERSIAN_SUPPORT
	OSD_SetTextFieldContent(pTxt,STRING_UNICODE,(UINT32)str);
	#else
	OSD_SetTextFieldContent(pTxt,STRING_UTF8,(UINT32)str_buff);
	#endif
	OSD_DrawObject((POBJECT_HEAD)pTxt,C_UPDATE_ALL);
}

extern UINT16 cur_image_idx;
void win_set_image_multiview_firstidx(void)
{
	image_multiview_first = cur_image_idx;
}

#endif

